import {
  getcontract,
  getclient,
  getclientinfo,
  getParameter,
  getPrice,
} from '@/api/trade'
import Vue from 'vue'
export default {
  // namespaced:true,
  state: {
    contract_list:[],//合约数组
    client_list:[],//用户数组
    contract:'',//可交易合约
    contract_all:'',//所有合约
    client:'',//用户
    client_info:{},//用户基本信息、风险控制
    strategy:'循环策略',//策略
    base_paramter:[],//基础参数
    batch_paramter:[],//批量参数
    run_select_Ids:[],//已启动运行选中数组
    unrun_select_Ids:[],//未启动运行选中数组
    price:{
      price_now:'',//最新价格
      price_index:'',//指数价格
      price_ema:'',//ema价格
      price_of_stop_profit_and_stop_loss:'',//系统使用的价格
    },//价格
    select_id_lock:false,//策略锁
    Interval_getClientInfo:'',//定时请求用户基本信息、风险控制
    Interval_get_price:'',//定时请求用户基本信息、风险控制
    run_status:true, //已启动和未启动切换
    start_time:'',
    end_time:'',
  },
  mutations: {
    set_contract(state, contract) {
      if(JSON.stringify(state.contract) !== JSON.stringify(contract)){
        state.contract = JSON.parse(JSON.stringify(contract))
      }
    },
    set_contract_all(state, contract_all) {
      if(JSON.stringify(state.contract_all) !== JSON.stringify(contract_all)){
        state.contract = JSON.parse(JSON.stringify(contract))
      }
    },
    set_client(state, client) {
      if(JSON.stringify(state.client) !== JSON.stringify(client)){
        state.client = JSON.parse(JSON.stringify(client))
      }
    },
    set_client_list(state, client_list) {
      state.client_list = client_list
    },
    set_contract_list(state, contract_list) {
      state.contract_list = contract_list
    },
    set_client_info(state, client_info) {
      state.client_info = client_info
    },
    set_strategy(state, strategy) {
      state.strategy = strategy
    },
    set_base_paramter(state, base_paramter) {
      if(JSON.stringify(state.base_paramter) !== JSON.stringify(base_paramter)){
        state.base_paramter = JSON.parse(JSON.stringify(base_paramter))
      }
    },
    set_batch_paramter(state, batch_paramter) {
      if(JSON.stringify(state.batch_paramter) !== JSON.stringify(batch_paramter)){
        state.batch_paramter = JSON.parse(JSON.stringify(batch_paramter))
      }
    },
    set_run_select_Ids(state, run_select_Ids) {
        if(JSON.stringify(state.run_select_Ids) !== JSON.stringify(run_select_Ids)){
          state.run_select_Ids = JSON.parse(JSON.stringify(run_select_Ids))
        }
    },
    set_unrun_select_Ids(state, unrun_select_Ids) {
      if(JSON.stringify(state.unrun_select_Ids) !== JSON.stringify(unrun_select_Ids)){
        state.unrun_select_Ids = JSON.parse(JSON.stringify(unrun_select_Ids))
      }
    },
    set_select_id_lock(state, select_id_lock) {
      if(JSON.stringify(state.select_id_lock) !== JSON.stringify(select_id_lock)){
        state.select_id_lock = JSON.parse(JSON.stringify(select_id_lock))
      }
    },
    set_price(state, price) {
      if(JSON.stringify(state.price) !== JSON.stringify(price)){
        if(price !== undefined){
          state.price = JSON.parse(JSON.stringify(price))
        }
      }
    },
    set_run_status(state, run_status) {
      state.run_status = JSON.parse(JSON.stringify(run_status))
    },
    set_start_time(state, start_time) {
      state.start_time = JSON.parse(JSON.stringify(start_time))
    },
    set_end_time(state, end_time) {
      state.end_time = JSON.parse(JSON.stringify(end_time))
    },
  },
  getters: {
    get_price:state =>state.price,
    get_contract:state =>state.contract,
    get_client:state =>state.client,
    get_client_list:state =>state.client_list,
    get_contract_list:state =>state.contract_list,
    get_client_info:state =>state.client_info,
    get_strategy:state =>state.strategy,
    get_base_paramter:state =>state.base_paramter,
    get_batch_paramter:state =>state.batch_paramter,
    get_run_select_Ids:state =>state.run_select_Ids,
    get_unrun_select_Ids:state =>state.unrun_select_Ids,
    get_select_id_lock:state =>state.select_id_lock,
    get_Interval_getClientInfo:state =>state.Interval_getClientInfo,
    get_Interval_get_price:state =>state.Interval_get_price,
    get_run_status:state =>state.run_status,
    get_start_time:state =>state.start_time,
    get_end_time:state =>state.end_time,

  },
  actions: {
    //获取合约信息
    getcontract({ commit }, {client}){
      return new Promise((resolve, reject) => {
        getcontract({client}).then(res => {
          const data = res.data;
          if(data.code == 1000){
            commit('set_contract_list', data.data.list_contract);
            commit('set_contract', data.data.list_contract[0]);
            resolve('成功获取合约')
          }else{
            reject(data.data.msg)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },

    //获取所有合约信息
    getcontract_all({ commit }, {client}){
      return new Promise((resolve, reject) => {
        getcontract({client}).then(res => {
          const data = res.data;
          if(data.code == 1000){
            commit('set_contract_all', data.data);
            resolve('成功获取合约')
          }else{
            reject(data.data.msg)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    //获取用户信息
    getClient({ commit }, {}){
      return new Promise((resolve, reject) => {
        getclient({}).then(res => {
          const data = res.data;
          if(data.code == 1000){
            commit('set_client_list', data.data.list_client);
            commit('set_client', data.data.list_client[0]);
            resolve('成功获取用户信息')
          }else{
            reject(data.data.msg)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    //获取用户基本信息、风险控制
    getClientInfo({ state,commit }, {client,contract}){
      return new Promise((resolve, reject) => {
        getclientinfo({client,contract}).then(res => {
          const data = res.data;
          if(data.code == 1000){
            commit('set_client_info', data.data);
            resolve('成功获取用户基本信息、风险控制')
          }else{
            // clearInterval(state.Interval_getClientInfo );
            reject(data.data.msg)
          }
        }).catch(err => {
          // clearInterval(state.Interval_getClientInfo );
          reject(err)
        })
      })
    },
    //定时获取用户基本信息、风险控制
    getClientInfoPro({ state,dispatch }, {client,contract}){
      clearInterval(state.Interval_getClientInfo );
      state.Interval_getClientInfo = setInterval(() =>{dispatch('getClientInfo', {client,contract})}, 3000);
      return dispatch('getClientInfo', {client,contract});
    },
    stopClientInfoPro({ state,dispatch }){
      clearInterval(state.Interval_getClientInfo );
    },
    //获取基础参数
    getBaseParamter({ state,commit }, {client,contract}){
      return new Promise((resolve, reject) => {
        let strategy = state.strategy;
        let basic_or_batch = 'basic';//基础
        getParameter({client,contract,strategy,basic_or_batch}).then(res => {
          const data = res.data;
          if(data.code == 1000){
            commit('set_base_paramter', data.data);
            resolve('成功获取基础参数')
          }else{
            reject("基础参数:"+data.data.msg)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    //获取批量参数
    getBatchParamter({ state,commit }, {client,contract}){
      return new Promise((resolve, reject) => {
        let strategy = state.strategy;
        let basic_or_batch = 'batch';//批量
        getParameter({client,contract,strategy,basic_or_batch}).then(res => {
          const data = res.data;
          // console.log(data);
          if(data.code == 1000){
            commit('set_batch_paramter', data.data);
            resolve('成功获取批量参数')
          }else{
            reject("批量参数:"+data.data.msg)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    //获取最新价格，指数价格
    getPrice({ state,commit }, {}){
      return new Promise((resolve, reject) => {
        getPrice({}).then(res => {
          const data = res.data;
          if(data.code == 1000){
            commit('set_price', data.data[''+state.contract+'']);
            resolve('成功获取最新价格，指数价格')
          }else{
            // clearInterval(state.Interval_get_price );
            reject(data.data.msg)
          }
        }).catch(err => {
          // clearInterval(state.Interval_get_price );
          reject(err)
        })
      })
    },
    //定时获取价格
    getPricePro({ state,dispatch }, {}){
      clearInterval(state.Interval_get_price );
      state.Interval_get_price = setInterval(() =>{dispatch('getPrice', {})}, 10000);
      return dispatch('getPrice', {});
    },
    //退出清空信息
    trade_logOut({ state, commit }){
      clearInterval(state.Interval_getClientInfo );
      clearInterval(state.Interval_get_price );
      commit('set_contract','');
      commit('set_client','');
      commit('set_client_list',[]);
      commit('set_contract_list',[]);
      commit('set_client_info','');
      commit('set_strategy','');
      commit('set_base_paramter',[]);
      commit('set_batch_paramter',[]);
      commit('set_run_select_Ids',[]);
      commit('set_unrun_select_Ids',[]);
      commit('set_run_status',true);
      commit('set_price',{
        now:'',//最新价格
        index:'',//指数价格
      },);
    }
  }
}
